<?php
/**
 ** added by lss@2017/07/14
 ** 获取最新一期开奖数据，比对数据库，生成中/不中记录
**/
set_time_limit(0);
date_default_timezone_set("PRC");
include('./db_config.php');
include('./function.php');

$type = 20; //北京赛车对应数据库中的dat_type
//1.连接数据库->pdo方式
$db_pdo_link = pdo_write_read_link();
$api_url = "http://c.apiplus.net/newly.do?token=E0C5726B8E45A086&format=json&code=bjpk10&rows=1";  //调取北京赛车最新数据的

//2.获取公式
$file_url = 'algo_data.txt';
$algo_data = file_get_contents($file_url);
if(!empty($algo_data))
{
	$algo_data = unserialize($algo_data);
}else
{
	$sql = "select `algo_id` from  pk10_referral_algo";
	$algo_result = $db_pdo_link->query($sql);
	$algo_data = $algo_result->fetchAll(PDO::FETCH_ASSOC);
	if(count($algo_data)>=0)
	{
		file_put_contents($file_url,serialize($algo_data));
	}
}

if(!is_array($algo_data) || empty($algo_data))
{
	exit("获取公式失败");	
}

//3.定义计划种类
$guess_type_array = array('gjw','yjw','jjw','sjw','wjw','ljw','qjw','bjw','njw','tjw','q2zx','q3zx','gyhz','gyhzdx','gyhzds');

//4.调用最新一期开奖数据
$newest_expect = json_decode(fopen_url($api_url),true);
if(empty($newest_expect))
{
	return false;
}
/*
stdClass Object
(
    [rows] => 1
    [code] => bjpk10
    [remain] => 2779hrs
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [expect] => 628726
                    [opencode] => 09,05,10,07,02,06,04,03,08,01
                    [opentime] => 2017-07-14 18:23:02
                    [opentimestamp] => 1500027782
                )

        )

)
*/
$lottery_code = $newest_expect['data'][0]['opencode'];
$lottery_expect = $newest_expect['data'][0]['expect'];

//首先查询是否已经运行过了，如果运行过这个期数的数据了，则不要再运行了
$last_expect = file_get_contents('last_expect.txt');
if($last_expect == $lottery_expect)
{
	exit('already done');
}else
{
	file_put_contents('last_expect.txt',$lottery_expect);
}

foreach($algo_data as $algo_single) //1000个公式循环
{
	echo "/usr/local/php/bin/php set_pk10_match_single.php  {$algo_single["algo_id"]}  {$lottery_expect}  {$lottery_code}";
	//echo '<br/>';
	$last_line = system("/usr/local/php/bin/php set_pk10_match_single.php  {$algo_single["algo_id"]}  {$lottery_expect}  {$lottery_code}",$retval);	
}









?>
